
package com.sc.datacollect.auth;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.sc.datacollect.util.Common;

/**
 * 安全认证
 * 
 * @author dong
 *
 */
public class AuthFilter {

	private String date;

	public AuthFilter(String massage) {
		this.date = massage;
	}

	/**
	 * 数据认证
	 * 
	 * @throws Exception
	 */
	public String authmessage() throws Exception {
		return AuthFilter.messageReceived(date);
	}

	public static String messageReceived(String message) throws Exception {

		if (!isAuthSuccess(message)) {
			return Common.AUTHERRORCODE;
		} else {
			return null;
		}
	}

	/**
	 * 是否认证成功
	 * 
	 * @param map
	 * @return
	 */
	private static boolean isAuthSuccess(String map) {
		JSONObject json = JSONObject.parseObject(map);
		String appId = (String) json.get("appId");
		String appKey = (String) json.get("appKey");
		boolean isAuth = false;
		for (Map<String, Object> authMap : AuthConfig.getInstance().getAuthentications()) {
			if (appId.equals(authMap.get("appId")) && appKey.equals(authMap.get("appKey"))) {
				isAuth = true;
				break;
			}
		}
		return isAuth;
	}
}
